#include "mutex_lock.h"

#ifdef MutexLockGuard
#undef MutexLockGuard
#endif

namespace mars {

// ---- MutexLock ----
MutexLock::MutexLock() {
    pthread_mutex_init(&_mutex, NULL);
}

MutexLock::~MutexLock() {
    pthread_mutex_destroy(&_mutex);
}

void MutexLock::lock() {
    pthread_mutex_lock(&_mutex);    
}

void MutexLock::unlock() {
    pthread_mutex_unlock(&_mutex);    
}

pthread_mutex_t* MutexLock::get_pthread_mutex() {
    return &_mutex;
}

// ---- MutexLockGuard ----
MutexLockGuard::MutexLockGuard(MutexLock& lock): _lock(lock) {
    _lock.lock();
}
MutexLockGuard::~MutexLockGuard() {
    _lock.unlock();
}

}// namespace mars
